//
//  LKMixController.m
//  lkmedia_iOS
//
//  Created by VictorLiu on 2018/10/21.
//  Copyright © 2018 VictorLiu. All rights reserved.
//

#import "LKMixController.h"

@interface LKMixController ()

@property (weak, nonatomic) IBOutlet UIButton *audioButton;

@property (nonatomic, strong) NSString *videoPath;

@property (nonatomic, strong) NBRecordAudioModel *audioModel;

@property (nonatomic, strong) NBVideoModel *mixVideoModel;

@property (weak, nonatomic) IBOutlet UILabel *mixNameLabel;

@property (weak, nonatomic) IBOutlet UIButton *mixPlayButton;


@end

@implementation LKMixController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ihaveapen" ofType:@"mp4"];
    self.videoPath = path;
}

#pragma mark --- 事件
/** 播放视频 */
- (IBAction)playVideoButtonClicked:(id)sender {
    
    __weak typeof(self) weakSelf = self;
    [NBAVTool playVideoPath:self.videoPath withCompletionHandle:^(BOOL success, NSString *errorStr) {
        if (!success) {
            [weakSelf.view makeToast:errorStr];
        }
    }];
}


- (IBAction)recorderButtonTouchDown:(UIButton *)sender {
    [sender setTitle:@"松开停止录音" forState:UIControlStateNormal];
    sender.backgroundColor = [UIColor lightGrayColor];
    __weak typeof(self) weakSelf = self;
    [NBAVTool startRecordAudio:^(BOOL success, NSString *errorStr) {
        if (!success) {
            [weakSelf.view makeToast:errorStr];
        }
    }];
}

- (IBAction)recorderButtonTouchUpInside:(UIButton *)sender {
    [sender setTitle:@"按住说话" forState:UIControlStateNormal];
    sender.backgroundColor = [UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1];
    __weak typeof(self) weakSelf = self;
    [NBAVTool stopRecordAudio:^(BOOL success, NSString *errorStr, NBRecordAudioModel *audio) {
        if (success) {
            weakSelf.audioModel = audio;
            weakSelf.audioButton.hidden = NO;
            [weakSelf.audioButton setTitle:audio.fileName forState:UIControlStateNormal];
        } else {
            [weakSelf.view makeToast:errorStr];
        }
    }];
}

- (IBAction)recorderButtonTouchUpOutSide:(UIButton *)sender {
    
    [sender setTitle:@"按住说话" forState:UIControlStateNormal];
    sender.backgroundColor = [UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1];
    __weak typeof(self) weakSelf = self;
    [NBAVTool stopRecordAudio:^(BOOL success, NSString *errorStr, NBRecordAudioModel *audio) {
        if (success) {
            weakSelf.audioModel = audio;
            weakSelf.audioButton.hidden = NO;
            [weakSelf.audioButton setTitle:audio.fileName forState:UIControlStateNormal];
            
        } else {
            [weakSelf.view makeToast:errorStr];
        }
    }];
}


- (IBAction)audioButtonClicked:(id)sender {
    __weak typeof(self) weakSelf = self;
    [NBAVTool playAudioPath:self.audioModel.path withCompletionHandle:^(BOOL success, NSString *errorStr) {
        if (!success) {
            [weakSelf.view makeToast:errorStr];
        }
    }];
}

- (IBAction)mergeButtonClicked:(id)sender {
    
    if (!self.audioModel) {
        [self.view makeToast:@"请先录制音频"];
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    [NBAVTool mergeAudio:self.audioModel.path andVideo:self.videoPath completionHandle:^(BOOL success, NSString *errorStr, NBVideoModel *video) {
        if (success) {
            weakSelf.mixVideoModel = video;
            weakSelf.mixNameLabel.hidden = NO;
            weakSelf.mixPlayButton.hidden = NO;
            weakSelf.mixNameLabel.text = video.fileName;
        } else {
            [weakSelf.view makeToast:errorStr];
        }
    }];
}

- (IBAction)mixPlayButtonClicked:(id)sender {
    if (!self.mixVideoModel) {
        [self.view makeToast:@"请先合成视频"];
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    [NBAVTool playVideoPath:self.mixVideoModel.path withCompletionHandle:^(BOOL success, NSString *errorStr) {
        if (!success) {
            [weakSelf.view makeToast:errorStr];
        }
    }];
}


@end
